package com.yuexunit.operationman.nbsy.module.OperationBeforeAndAfter.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationBeforeAndAfter.model.impl.BeforeAndAfterModel;
import com.yuexunit.operationman.nbsy.module.OperationBeforeAndAfter.presenter.IBeforeAndAfterPresenter;
import com.yuexunit.operationman.nbsy.module.OperationBeforeAndAfter.presenter.impl.BeforeAndAfterPresenter;
import com.yuexunit.operationman.nbsy.module.OperationBeforeAndAfter.ui.view.IBeforeAndAfterView;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.ClickDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/11/9 0009.
 */

public class Act_OperationBeforeAndAfter_AfterVisit extends BaseKJActivity implements IBeforeAndAfterView, ISafeOperationSignView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.bv_01)
    EditText bv01;
    @Bind(R.id.bv_02)
    EditText bv02;
    @Bind(R.id.bv_03)
    EditText bv03;
    @Bind(R.id.bv_04)
    EditText bv04;
    @Bind(R.id.bv_0501)
    RadioButton bv0501;
    @Bind(R.id.bv_0502)
    RadioButton bv0502;
    @Bind(R.id.bv_0503)
    RadioButton bv0503;
    @Bind(R.id.bv_0601)
    CheckBox bv0601;
    @Bind(R.id.bv_0602)
    CheckBox bv0602;
    @Bind(R.id.bv_0603)
    CheckBox bv0603;
    @Bind(R.id.bv_0604)
    CheckBox bv0604;
    @Bind(R.id.bv_0701)
    CheckBox bv0701;
    @Bind(R.id.bv_0702)
    CheckBox bv0702;
    @Bind(R.id.bv_0703)
    CheckBox bv0703;
    @Bind(R.id.bv_0801)
    RadioButton bv0801;
    @Bind(R.id.bv_0802)
    RadioButton bv0802;
    @Bind(R.id.bv_0901)
    RadioButton bv0901;
    @Bind(R.id.bv_0902)
    RadioButton bv0902;
    @Bind(R.id.bv_1001)
    RadioButton bv1001;
    @Bind(R.id.bv_1002)
    RadioButton bv1002;
    @Bind(R.id.bv_1003)
    RadioButton bv1003;
    @Bind(R.id.bv_11)
    EditText bv11;

    //签名
    @Bind(R.id.img_sign_aftervisit)
    ImageView imgSignAftervisit;
    @Bind(R.id.tv_sign_aftervisit_time)
    TextView tvSignAftervisitTime;
    @Bind(R.id.tv_sign_aftervisit_name)
    TextView tvSignAftervisitName;
    @Bind(R.id.btn_sign_aftervisit)
    TextView btnSignAftervisit;

    LoadingDialog loadingDialog;
    IBeforeAndAfterPresenter presenter;

    LoginDialog loginDialog;
    ISafeOperationSignPresenter presenter_sign;

    ClickDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_operationbeforeandafter_aftervisit);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(this);
        presenter = new BeforeAndAfterPresenter(this, new BeforeAndAfterModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getName());
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.BEFOREANDAFTERBEAN.getObject().getSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.BEFOREANDAFTERBEAN.getObject().getVisitNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent() {
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignTime().length() > 0) {
            imgSignAftervisit.setVisibility(View.VISIBLE);
            btnSignAftervisit.setVisibility(View.GONE);
            tvSignAftervisitTime.setVisibility(View.VISIBLE);
            tvSignAftervisitName.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignURL())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgSignAftervisit);
            tvSignAftervisitTime.setText("签名时间:" + AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignTime());
            tvSignAftervisitName.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignName());
        }

        bv01.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getOperationName());
        bv02.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getPostoperative());
        bv03.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getPostoperativeDiagnosis());
        bv04.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getTemperature());

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getPain().equals("0")) {
            bv0501.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getPain().equals("1")) {
            bv0502.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getPain().equals("2")) {
            bv0503.setChecked(true);
        }

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getWound().length() > 0) {
            String[] arrays = AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getWound().split(";", -1);
            if (arrays[0].equals("1")) {
                bv0601.setChecked(true);
            }
            if (arrays[1].equals("1")) {
                bv0602.setChecked(true);
            }
            if (arrays[2].equals("1")) {
                bv0603.setChecked(true);
            }
            if (arrays[3].equals("1")) {
                bv0604.setChecked(true);
            }
        }

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getRecure().length() > 0) {
            String[] arrays = AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getRecure().split(";", -1);
            if (arrays[0].equals("1")) {
                bv0701.setChecked(true);
            }
            if (arrays[1].equals("1")) {
                bv0702.setChecked(true);
            }
            if (arrays[2].equals("1")) {
                bv0703.setChecked(true);
            }
        }

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getCareForYou().equals("0")) {
            bv0801.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getCareForYou().equals("1")) {
            bv0802.setChecked(true);
        }

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getHelpYou().equals("0")) {
            bv0901.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getHelpYou().equals("1")) {
            bv0902.setChecked(true);
        }

        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getAttitude().equals("0")) {
            bv1001.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getAttitude().equals("1")) {
            bv1002.setChecked(true);
        }
        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getAttitude().equals("2")) {
            bv1003.setChecked(true);
        }

        bv11.setText(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSuggestion());
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.titlebar_docadvice_img_back_ll, R.id.sure
            , R.id.btn_sign_aftervisit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.titlebar_docadvice_img_back_ll:
                finish();
                break;
            case R.id.sure:
                if(AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignName()!=null&&
                        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().getSignName().length()>0) {
                    insertContent();
                }
                else {
                    dialog = new ClickDialog(aty).builder().setTxt("存在未签名,是否提交？");
                    dialog.isCancleBack(true);
                    dialog.setCallBack(new ClickDialog.ICallBack() {
                        @Override
                        public void callback(Bundle bundle) {
                            if(bundle.getInt("data") == 0){
                                insertContent();
                            } else if(bundle.getInt("data") == 1){
                                dialog.dismiss();
                            }

                        }
                    });
                    dialog.show();
                }
                break;
            case R.id.btn_sign_aftervisit:
                showTypeDialog();
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            if (signBean.getObject().getDoctorType().equals("H")) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                imgSignAftervisit.setVisibility(View.VISIBLE);
                Glide.with(this)
                        .load(signBean.getObject().getSignPicture())
                        .placeholder(R.color.white)
                        .error(R.color.white)
                        .into(imgSignAftervisit);
                btnSignAftervisit.setVisibility(View.GONE);
                tvSignAftervisitTime.setVisibility(View.VISIBLE);
                tvSignAftervisitTime.setText("签名时间:" + str);
                tvSignAftervisitName.setVisibility(View.VISIBLE);
                tvSignAftervisitName.setText(signBean.getObject().getDoctorName());

                AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setSignName(signBean.getObject().getDoctorName());
                AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setSignURL(signBean.getObject().getSignPicture());
                AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setSignTime(str);
            } else {
                ViewInject.toast("请使用护士账号进行签名");
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    private void insertContent() {
        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setOperationName(bv01.getText().toString());
        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setPostoperative(bv02.getText().toString());
        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setPostoperativeDiagnosis(bv03.getText().toString());
        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setTemperature(bv04.getText().toString());

        if (bv0501.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setPain("0");
        }
        if (bv0502.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setPain("1");
        }
        if (bv0503.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setPain("2");
        }

        if (bv0601.isChecked() || bv0602.isChecked() || bv0603.isChecked() || bv0604.isChecked()) {
            String arrays = "";
            if (bv0601.isChecked()) {
                arrays += "1;";
            } else {
                arrays += ";";
            }
            if (bv0602.isChecked()) {
                arrays += "1;";
            } else {
                arrays += ";";
            }
            if (bv0603.isChecked()) {
                arrays += "1;";
            } else {
                arrays += ";";
            }
            if (bv0604.isChecked()) {
                arrays += "1";
            } else {
                arrays += "";
            }
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setWound(arrays);
        }

        if (bv0701.isChecked() || bv0702.isChecked() || bv0703.isChecked()) {
            String arrays = "";
            if (bv0701.isChecked()) {
                arrays += "1;";
            } else {
                arrays += ";";
            }
            if (bv0702.isChecked()) {
                arrays += "1;";
            } else {
                arrays += ";";
            }
            if (bv0703.isChecked()) {
                arrays += "1";
            } else {
                arrays += "";
            }
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setRecure(arrays);
        }

        if (bv0801.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setCareForYou("0");
        }
        if (bv0802.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setCareForYou("1");
        }

        if (bv0901.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setHelpYou("0");
        }
        if (bv0902.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setHelpYou("1");
        }

        if (bv1001.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setAttitude("0");
        }
        if (bv1002.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setAttitude("1");
        }
        if (bv1003.isChecked()) {
            AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setAttitude("2");
        }

        AppConfig.BEFOREANDAFTERBEAN.getObject().getAfterNursing().setSuggestion(bv11.getText().toString());


        if (AppConfig.BEFOREANDAFTERBEAN.getObject().getObjectId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.BEFOREANDAFTERBEAN.getObject()));
            presenter.insertBeforeAndAfterDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.BEFOREANDAFTERBEAN.getObject()));
            presenter.updateBeforeAndAfterDate(params);
        }
    }

    @Override
    public void insertBeforeAndAfterDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateBeforeAndAfterDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
